package com.portal.cwegportal.controller;
import com.appcenter.jz.Logger.BusinessLogger;
import com.appcenter.jz.Logger.LoggerGenre;
import com.appcenter.jz.config.ErrorJsonResult;
import com.appcenter.jz.config.JsonResult;
import com.portal.cwegportal.input.publicationannouncement.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("api/services/portal/publicationannouncement")
@Api(description ="发文/公告API")
public class PublicationAnnouncementController {
    @Autowired
    com.portal.cwegportal.service.PublicationAnnouncementService publicationAnnouncementService;

    @ApiOperation(value = "創建发文公告", notes = "創建发文公告", produces = "application/json")
    @PostMapping("/createPublicationAnnouncement")
    public ResponseEntity<JsonResult> createPublicationAnnouncement(@RequestBody CreatePublicationAnnouncementInput input) throws Exception {
        //throw new ErrorJsonResult("1000001", "捕获异常信息");
        BusinessLogger.Info( LoggerGenre.Add,"創建发文公告",input);
        JsonResult r = new JsonResult();

        try {
            r.setResult(publicationAnnouncementService.createPublicationAnnouncement(input));
        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }
        return ResponseEntity.ok(r);
    }


    @ApiOperation(value = "删除發文公告", notes = "删除發文公告", produces = "application/json")
    @PostMapping("/deletePublicationAnnouncement")
    public ResponseEntity<JsonResult> deletePublicationAnnouncement(@RequestBody DeletePublicationAnnouncementInput input) throws Exception {
        //throw new ErrorJsonResult("1000001", "捕获异常信息");
        BusinessLogger.Info( LoggerGenre.Add,"删除發文公告",input);
        JsonResult r = new JsonResult();
        try {
            r.setResult(publicationAnnouncementService.deletePublicationAnnouncement(input));

        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }
        return ResponseEntity.ok(r);
    }


    @ApiOperation(value = "修改發文公告", notes = "修改發文公告", produces = "application/json")
    @PostMapping("/editPublicationAnnouncement")
    public ResponseEntity<JsonResult> editPublicationAnnouncement(@RequestBody EditPublicationAnnouncementInput input) throws Exception {
        //throw new ErrorJsonResult("1000001", "捕获异常信息");
        BusinessLogger.Info( LoggerGenre.Add,"修改發文公告",input);
        JsonResult r = new JsonResult();
        try {
            r.setResult(publicationAnnouncementService.editPublicationAnnouncement(input));
        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }
        return ResponseEntity.ok(r);
    }

    @ApiOperation(value = "查詢發文公告", notes = "查詢發文公告", produces = "application/json")
    @PostMapping("/getPublicationAnnouncement")
    public ResponseEntity<JsonResult> getPublicationAnnouncement(@RequestBody GetPublicationAnnouncementInput input) throws Exception {
        //throw new ErrorJsonResult("1000001", "捕获异常信息");
        BusinessLogger.Info( LoggerGenre.Add,"查詢發文公告",input);
        JsonResult r = new JsonResult();

        try {
            r.setResult(publicationAnnouncementService.getPublicationAnnouncement(input));
        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }

        return ResponseEntity.ok(r);
    }

    @ApiOperation(value = "加载發文公告", notes = "加载發文公告", produces = "application/json")
    @PostMapping("/pageLoad")
    public ResponseEntity<JsonResult> pageLoad(@RequestBody PageLoadInput input) throws Exception {
        BusinessLogger.Info( LoggerGenre.Add,"加载發文公告",input);
        JsonResult r = new JsonResult();
        try {
            r.setResult(publicationAnnouncementService.pageLoad(input));
        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }
        return ResponseEntity.ok(r);
    }

    @ApiOperation(value = "通过id查询發文公告", notes = "通过id查询發文公告", produces = "application/json")
    @PostMapping("/getPublicationAnnouncementById")
    public ResponseEntity<JsonResult> getPublicationAnnouncementById(@RequestBody GetPublicationAnnouncementInput input) throws Exception {
        //throw new ErrorJsonResult("1000001", "捕获异常信息");
        BusinessLogger.Info( LoggerGenre.Add,"通过id查询發文公告",input);
        JsonResult r = new JsonResult();

        try {
            r.setResult(publicationAnnouncementService.getPublicationAnnouncementById(input));
        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }

        return ResponseEntity.ok(r);
    }

    @ApiOperation(value = "首页查詢新闻/学院天地", notes = "首页查詢新闻/学院天地", produces = "application/json")
    @PostMapping("/getHomePageNewsCollege")
    public ResponseEntity<JsonResult> getHomePagePublicationAnnouncement(@RequestBody GetHomePagePublicationAnnouncementInput input) throws Exception {
        //throw new ErrorJsonResult("1000001", "捕获异常信息");
        BusinessLogger.Info( LoggerGenre.Add,"首页查詢新闻/学院天地",input);
        JsonResult r = new JsonResult();

        try {
            r.setResult(publicationAnnouncementService.getHomePageNewsCollege(input));
        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }

        return ResponseEntity.ok(r);
    }
}